package com.example.second_home_work

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Spinner
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {

    private lateinit var expert: ProgramExpert
    private lateinit var linearLayout: LinearLayout
    private lateinit var spinner: Spinner
    private lateinit var resultTextView: TextView

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }


        expert = ProgramExpert()

        // 区1: 动态添加 TextView
        var count = 0
        val button2 = findViewById<Button>(R.id.button2)
        linearLayout = findViewById(R.id.LinearLayout)
        button2.setOnClickListener {
            val textView = TextView(this)
            count++
            textView.text = "New TextView $count!"
            linearLayout.addView(textView)
        }

        // 区2: MVC 模式下的事件响应
        val button = findViewById<Button>(R.id.button)
        val linearLayout2 = findViewById<LinearLayout>(R.id.LinearLayout2)

        spinner = findViewById(R.id.spinner)

        resultTextView = TextView(this)
        linearLayout2.addView(resultTextView)
        button.setOnClickListener {
            val selectedFeature = spinner.selectedItem.toString()
            resultTextView.text = expert.getLanguage(selectedFeature)
        }
    }
}